package com.texaco.chapasFutbol.menu;

import org.andengine.engine.camera.Camera;
import org.andengine.entity.scene.menu.MenuScene;
import org.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.entity.scene.menu.item.TextMenuItem;

import android.opengl.GLES20;

import com.texaco.chapasFutbol.R;
import com.texaco.chapasFutbol.entidades.Game;
import com.texaco.chapasFutbol.resources.Resources;
import com.texaco.chapasFutbol.scene.SceneHandler;

public class ContextualGameMenu extends ChapasMenuScene implements IOnMenuItemClickListener {

	public static final int MENU_RESET = 0;
	public static final int MENU_PRINCIPAL = MENU_RESET + 1;
	public static final int MENU_CONTINUE = MENU_PRINCIPAL + 1;
	public static final int MENU_QUIT = MENU_CONTINUE + 1;
	
	private SceneHandler sceneHandler;
	
	public ContextualGameMenu(Game game, SceneHandler sceneHandler, Camera camera){
		super(game, camera);
		this.sceneHandler = sceneHandler;

		final TextMenuItem resetMenuItem = new TextMenuItem(MENU_RESET, 
				this.game.getActivity().getFontMap().get(Resources.FONT_OPTIONS),
				this.game.getActivity().getText(R.string.tMenuItemReset), 
				game.getActivity().getVertexBufferObjectManager());
		resetMenuItem.setBlendFunction(GLES20.GL_SRC_ALPHA, 
				GLES20.GL_ONE_MINUS_SRC_ALPHA);
		this.addMenuItem(resetMenuItem);

		final TextMenuItem principalMenuItem = new TextMenuItem(MENU_PRINCIPAL, 
				this.game.getActivity().getFontMap().get(Resources.FONT_OPTIONS),
				this.game.getActivity().getText(R.string.tMenuItemPpalMenu), 
				game.getActivity().getVertexBufferObjectManager());
		principalMenuItem.setBlendFunction(GLES20.GL_SRC_ALPHA, 
				GLES20.GL_ONE_MINUS_SRC_ALPHA);
		this.addMenuItem(principalMenuItem);
		
		final TextMenuItem continueMenuItem = new TextMenuItem(MENU_CONTINUE, 
				this.game.getActivity().getFontMap().get(Resources.FONT_OPTIONS),
				this.game.getActivity().getText(R.string.tMenuItemContinue), 
				game.getActivity().getVertexBufferObjectManager());
		continueMenuItem.setBlendFunction(GLES20.GL_SRC_ALPHA, 
				GLES20.GL_ONE_MINUS_SRC_ALPHA);
		this.addMenuItem(continueMenuItem);

		final TextMenuItem quitMenuItem = new TextMenuItem(MENU_QUIT, 
				this.game.getActivity().getFontMap().get(Resources.FONT_OPTIONS),
				this.game.getActivity().getText(R.string.tMenuItemQuit), 
				game.getActivity().getVertexBufferObjectManager());
		quitMenuItem.setBlendFunction(GLES20.GL_SRC_ALPHA, 
				GLES20.GL_ONE_MINUS_SRC_ALPHA);
		this.addMenuItem(quitMenuItem);

		this.setBackgroundEnabled(false);
		this.buildAnimations();
		this.setOnMenuItemClickListener(this);
	}

	public boolean onOptionSelected(final MenuScene pMenuScene, 
			final IMenuItem pMenuItem, final float pMenuItemLocalX, 
			final float pMenuItemLocalY) {
		
			switch(pMenuItem.getID()) {
				case MENU_RESET:
					
					game.cameraHandler.resetCameraMenu();
					sceneHandler.getGame().reset();
					sceneHandler.getGame().clearChildScene();
					game.loadGameScene();
					
					return true;
				case MENU_PRINCIPAL:
					game.cameraHandler.resetCameraMenu();
					sceneHandler.getGame().reset();
					sceneHandler.getGame().clearChildScene();
					this.postActivateHud(false);
					game.finalizarJuego();
					game.loadMenuPrincipalScene();
					return true;
				case MENU_CONTINUE:
					return true;
				case MENU_QUIT:

					/* Confirmar el fin de la actividad. */
			        game.getActivity().showMessage(
			        		game.getActivity().DIALOG_CONFIRMATION_ID);
			        
					return true;
				default:
					return false;
			}
	}
}
